<?php

namespace App\Models;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
#项目信息表模型
class Project_info extends Model
{
	#主键
	public $primaryKey='pid';
	/**
	 * 表明模型是否应该被打上时间戳
	 *
	 * @var bool
	 */
	public $timestamps = false;





	#每个项目拥有一名老师，一对一
	public function teacher(){
		return $this->belongsTo('App\User','tid','uid');
	}
	#每个项目拥有一名组长，一对一
	public function std_manger(){
		return $this->hasOne('App\User','uid','std_mag');
	}
	#每个项目可被多人选择，一对多
	public function selection(){
		return $this->hasMany('App\Models\Selection','pid','pid');
	}

	#每个项目拥有一个费用清单，一对多
	public function fees(){
		return $this->hasOne('App\models\Fee','pid','pid');
	}

	#修改项目状态
	public static function setState($pid,$sid){
		$project_info=Project_info::find($pid);
		$project_info->type=$sid;
		$project_info->save();
	}

	#设置组长
	public static function setGuide($pid,$sid){
		$project_info=Project_info::find($pid);
		$project_info->std_mag=$sid;
		$project_info->save();
	}
	#确认开题
	public static function confirmProj($pid){
		$project_info=Project_info::find($pid);
		$project_info->editable=1;
		$project_info->save();
	}

	#发布新项目
	public static function newProj(Request $request){
		$project=new Project_info;
		$project->tid=$request->input('tid');                         #发布人
		$project->editable=0;                                         #初始化不可编辑0
		$project->support_type=$request->input('support_type');      #资助类别
		$project->std_mag=0;                                          #组长初始化未设置0
		$project->pname=$request->input('pname');                     #项目名
		$project->type=0;                                             #项目状态初始状态0
		$project->source=$request->input('source');                   #项目来源
		$project->start=$request->input('start');                     #项目开始时间
		$project->end=$request->input('end');                         #项目结束时间
		$project->fees=$request->input('fees');                       #费用
		$project->year=$request->input('year');                       #年份
		$project->college=$request->input('college');                 #学院
		$project->details=$request->input('details');                 #详细介绍
		$project->save();
		return $project;
	}

	#修改项目信息
	public static function modify_pro(Request $request,$pid){
		$project=Project_info::find($pid);
		$project->pname=$request->input('pname');
		$project->source=$request->input('source');
		$project->start=$request->input('start');                     #项目开始时间
		$project->end=$request->input('end');                         #项目结束时间
		$project->fees=$request->input('fees');                       #费用
		$project->details=$request->input('details');                 #详细介绍
		$project->save();
	}

}
